package com.masterclient.app.businessmodule.vipmodule.financial_management.contract;

import android.support.annotation.NonNull;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.FCBaseCallBack;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.utils.EncryptUtil;
import com.eoner.common.config.Config;
import com.masterclient.app.businessmodule.usermodule.api.UserHttpService;
import com.masterclient.app.businessmodule.usermodule.login.beans.CaptchaBean;
import com.masterclient.app.businessmodule.vipmodule.financial_management.api.FinancialService;
import com.masterclient.app.businessmodule.vipmodule.financial_management.bean.CashBaseInfoData;
import com.masterclient.app.utils.CaptchaUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class CashIndexPresenter extends CashIndexContract.Presenter{
    @Override
    public void getBaseInfo(String source, boolean showLoading) {
        if (showLoading){
            mView.baseShowPageLoading(false);
        }
        mRxManager.add(toNewSubscribe(FinancialService.getInstance().cashBaseInfo(source), new FCBaseCallBack<CommonBaseResponse<CashBaseInfoData>>() {
            @Override
            public void onSuccess(CommonBaseResponse<CashBaseInfoData> obj) {
                mView.showData(obj.getData());
                mView.baseHiddenPageLoading();
                //mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
            }

            @Override
            public void onServiceError(CommonBaseResponse<CashBaseInfoData> obj) {
                super.onServiceError(obj);
                mView.baseHiddenPageLoading();
                if (showLoading) {
                   // mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                }
            }

            @Override
            public void onNetWorkError(String errorMsg) {
                super.onNetWorkError(errorMsg);
                mView.baseHiddenPageLoading();
                if (showLoading) {
                  //  mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                }
            }
        }));
    }

    @Override
    public void applyCash(String amount, String bankcard_cert_id, String mobile, String captcha, String withdraw_source) {
        mView.baseShowPageLoading(false);
        mRxManager.add(toNewSubscribe(FinancialService.getInstance().cashApply(amount,bankcard_cert_id,mobile,captcha,withdraw_source), new FCBaseCallBack<CommonBaseResponse<Object>>() {
            @Override
            public void onSuccess(CommonBaseResponse obj) {
                mView.applyResult(true,obj.getMsg());
                mView.baseHiddenPageLoading();
            }

            @Override
            public void onServiceError(CommonBaseResponse obj) {
                mView.baseHiddenPageLoading();
                mView.applyResult(false,obj.getMsg());
            }

            @Override
            public void onNetWorkError(String errorMsg) {
                mView.baseHiddenPageLoading();
                mView.applyResult(true,errorMsg);
            }
        }));

    }

    @Override
    public void getCaptcha(@NonNull String mobile, @NonNull String type, String ne) {

        mView.baseShowCommitLoading("正在提交...");
        String captchaValidate = "";
        if (CaptchaUtil.CAPTCHA_NUMBER_TIMES >= Config.CAPTCHA_MAX_TIMES){
            Map<String,String> map = new HashMap<>();
            map.put("failureNum","3");
            map.put("clientTime",System.currentTimeMillis()/1000+"");
            captchaValidate = EncryptUtil.encryptRSA(new Gson().toJson(map), Config.captchaKey);
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile",mobile);
            jsonObject.put("type",type);
            jsonObject.put("NECaptchaValidate",ne);
            jsonObject.put("captchaValidate",captchaValidate);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                                              jsonObject.toString());
        Observable<CaptchaBean> observable = UserHttpService.getInstance().getCaptcha(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CaptchaBean>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull CaptchaBean bean) throws Exception {
                mView.baseHiddenCommitLoading();
                if("0".equals(bean.getCode())){
                    mView.captchaSuccess(true);
                }else {
                    ToastUtil.showToast(bean.getMsg());
                    mView.captchaSuccess(false);
                }
            }
        },new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenCommitLoading();
                mView.captchaSuccess(false);
                ToastUtil.showToast(WEB_FAILED_STR);
            }
        });
        mRxManager.add(disposable);
    }
}
